﻿using System;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Android.Dimen
{
    public partial class DimenForm : Form
    {
        public DimenForm()
        {
            InitializeComponent();
        }

        #region 生成目标适配XML和保存

        /// <summary>
        /// 生成目标适配XML和保存
        /// </summary>
        /// <param name="isSave">是否保存文件</param>
        private void DimenGenerate(bool isSave = false)
        {
            StringBuilder sb = new StringBuilder();

            var baseDpi = nudBaseDpi.Value;
            var targetDpi = nudTargetDpi.Value;
            var targetLength = nudTargetLength.Value;

            var targetMultiple = targetDpi / baseDpi;

            sb.AppendLine("<resources>");

            for (var i = 1; i <= targetLength; i++)
            {
                sb.AppendLine($"<dimen name=\"dimen{i}\">{(i * targetMultiple).ToString("f2")}dp</dimen>");
            }

            sb.AppendLine("</resources>");

            rtbOutputXml.Text = sb.ToString();

            //保存内容到文件
            if (!isSave) return;

            //开始保存内容到文件
            DimenGenerateToXml();

        }

        /// <summary>
        /// 生成目标适配保存到XML
        /// </summary>
        private void DimenGenerateToXml()
        {
            var baseDpi = nudBaseDpi.Value;
            var targetDpi = nudTargetDpi.Value;
            var targetLength = nudTargetLength.Value;
            var dimenLand = cbxDimenLand.Checked;

            var targetMultiple = targetDpi / baseDpi;

            //创建文件夹
            string path = $"{Application.StartupPath}\\res\\values-sw{targetDpi}dp{(dimenLand ? "-land" : "")}";
            if (false == System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
                if (false == System.IO.Directory.Exists(path))
                {
                    MessageBox.Show("创建文件夹路径失败！！");
                    return;
                }
            }

            //设置文件路径
            string file = $"{path}\\dimens.xml";

            //设置文件路径
            XmlDocument xmlDoc = new XmlDocument();
            //XML的声明<?xml version="1.0" encoding="gb2312"?> 
            //XmlDeclaration xmlSM = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);

            XmlElement xml = xmlDoc.CreateElement("", "resources", "");
            xmlDoc.AppendChild(xml);

            XmlNode resources = xmlDoc.SelectSingleNode("resources");

            for (var i = 1; i <= targetLength; i++)
            {
                XmlElement dimen = xmlDoc.CreateElement("dimen");
                dimen.SetAttribute("name", $"dimen{i}");
                dimen.InnerText = $"{(i * targetMultiple).ToString("f2")}dp";
                resources.AppendChild(dimen);
            }

            //保存好创建的XML文档
            xmlDoc.Save(file);
        }

        #endregion

        #region  生成目标适配XML

        /// <summary>
        /// 生成目标适配XML
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Start_Click(object sender, EventArgs e)
        {
            DimenGenerate();
        }

        /// <summary>
        /// 生成目标适配XML
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Start_Save_Click(object sender, EventArgs e)
        {
            DimenGenerate(true);
        }

        #endregion

    }
}
